use "$LocalData\BartikAnalyticData_2digit.dta", clear

* Set panel variables
xtset geography year

*Generating year-industry-shares for computation for Bartik analytics and SEs
		keep if year>2002  //Restricts to period of analysis
		
		levelsof year, local(y) //Creates a macro list of all years
		
		foreach year in `y' {
			foreach ind_var of varlist c_i_share_2000* {
				qui gen t`year'_`ind_var' = `ind_var' * (year == `year')
				}
			}
	
		qui tab year, gen(year_) //Generates year dummies
		drop year_1
		qui tab geography, gen(geography_) //Generates year dummies
		drop geography_1
		

/******************************************************************************/
/******************************************************************************/
/* Regressions                                                                */
/******************************************************************************/
/******************************************************************************/

local years  (year>2002) & abs(gsmall_dep1)<5 & gSmallFirm1!=.
*local controls ln_pop_2000 unem_2000 urban_2000 lnincome2000 lnbr2000 small_dep_share2000
local controls 	dlngdp unem_change pop_growth annualchange 

gen testconditions = cond(`years',1,0)
bysort geography: egen insample = sum(test)
keep if insample>1

/**********************/
/* Simple Regressions */
/**********************/
noi display "Running simple regressions."

local years  (year>2002) & abs(gsmall_dep1)<5  & !missing(bartikGS1) & !missing(gSmallFirm1)
*local controls ln_pop_2000 unem_2000 urban_2000 lnincome2000 lnbr2000 small_dep_share2000
local controls 	dlngdp unem_change pop_growth annualchange 

eststo: xtivreg2 gsmall_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
gen mainsample = e(sample)

local years  mainsample==1


*Opening file to collect standard errors from Adao et al 2019
capture file close fh
file open fh  using "$LocalData\Tables\Adao_Standard_Errors_TableRFS3.tex", write replace
file write fh "\toprule" _n
file write fh  " (1) & (2)  & (3)  & (4)  & (5) & (6) & (7) & (8) & (9) & (10)" _n


noi display "Starting baseline regressions."
noi display " "

*Deposits
noi display "Deposit regressions."

local controls 	dlngdp unem_change pop_growth annualchange 

*(1)
eststo: areg gSmallFirm1 bartikGS1  i.year if `years', cluster(cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex replace ///
	ctitle(gSmallFirm) keep(bartikGS1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)

*(2)
eststo: xtivreg2 gsmall_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex append ///
	ctitle(SmallDepGr) keep(gSmallFirm1  gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	

	
*(3)
eststo: areg gsmall_dep1 bartikGS1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex append ///
	ctitle(SmallDepGr) keep(bartikGS1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)

	
	preserve
	

		keep if `years'
		local controls2 geography_* year_*
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao gsmall_dep1 bartikGS1 `controls2' if `filter', wmatrix(`weightmatrix') tsls(gSmallFirm1)

		local se2 =string(se0[1,1], "%9.3f")
		local ciL2 =string(ciL[1,1], "%9.3f")
		local ciH2 =string(ciH[1,1], "%9.3f")

		local se3 =string(se_alpha[1,1], "%9.3f")
		local ciL3 =string(ciL2SLS[1,1], "%9.3f")
		local ciH3 =string(ciH2SLS[1,1], "%9.3f")
	restore


*(4)
eststo: areg gSmallFirm1 bartikGS1 `controls' i.year if `years', cluster(cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex append ///
	ctitle(gSmallFirm) keep(bartikGS1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)

*(5)
eststo: xtivreg2 gsmall_dep1 `controls' year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1  gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	

	preserve

		keep if `years'
		local controls2 geography_* year_* `controls' 
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao gsmall_dep1 bartikGS1 `controls2' if `filter', wmatrix(`weightmatrix')  tsls(gSmallFirm1)

		local se6 =string(se0[1,1], "%9.3f")
		local ciL6 =string(ciL[1,1], "%9.3f")
		local ciH6 =string(ciH[1,1], "%9.3f")

		local se5 =string(se_alpha[1,1], "%9.3f")
		local ciL5 =string(ciL2SLS[1,1], "%9.3f")
		local ciH5 =string(ciH2SLS[1,1], "%9.3f")
	restore

	

*(6)
eststo: areg gsmall_dep1 bartikGS1 `controls' year_*  if `years', absorb(geography) cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1  bartikGS1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	

	
*(7)

	eststo: xtivreg2 gsmall_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years' [aweight=pop2000],  fe cluster(cluster3)
	local widstat = round(`e(widstat)',0.1)
	outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex append ///
		ctitle(Small Dep Gr) keep(gSmallFirm1  `controls' ) ///
		addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	gen wsample = e(sample)
	bysort geography: egen singletonw = sum(wsample)
	keep if wsample==1 & singletonw>1
				
preserve

		keep if `years'
		local controls2 geography_* year_* `controls' 
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao gsmall_dep1 bartikGS1 `controls2' if `filter', wmatrix(`weightmatrix')  tsls(gSmallFirm1)

		local se7 =string(se_alpha[1,1], "%9.3f")
		local ciL7 =string(ciL2SLS[1,1], "%9.3f")
		local ciH7 =string(ciH2SLS[1,1], "%9.3f")
	restore	
	
*(8)
eststo: areg gLargeFirm1 bartikGL1  i.year if `years', cluster(cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex append ///
	ctitle(gLargeFirm) keep(bartikGL1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	
*(9)
eststo: xtivreg2 gsmall_dep1 year_* (gLargeFirm1 = bartikGL1)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex append ///
	ctitle(SmallDepGr) keep(gSmallFirm1  gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	
	
	preserve

		keep if `years'
		local controls2 geography_* year_* 
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao gsmall_dep1 bartikGL1 `controls2' if `filter', wmatrix(`weightmatrix')  tsls(gLargeFirm1)

		local se9 =string(se_alpha[1,1], "%9.3f")
		local ciL9 =string(ciL2SLS[1,1], "%9.3f")
		local ciH9 =string(ciH2SLS[1,1], "%9.3f")
	restore
	
	

*(10)
eststo: xtivreg2 gsmall_dep1 year_* (gLargeFirm1 gSmallFirm1 = bartikGL1 bartikGS1)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_RFS3", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1  gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)

	

file write fh  "`se1' & `se2' & `se3' & `se4' & `se5' & `se6' & `se7' & `se8' & `se9' & `se10'   \\" _n
file write fh  " & [`ciL1', `ciH1'] & [`ciL2', `ciH2'] & [`ciL3', `ciH3'] & [`ciL4', `ciH4'] & [`ciL5', `ciH5'] & [0.050, 3.051] & [`ciL7', `ciH7'] & [`ciL8', `ciH8'] & [`ciL9', `ciH9'] & [`ciL10', `ciH10']  \\" _n
	
file close fh
		
noi display "Finished running Deposit regressions."
*noi etime
noi display " "
